package com.platform.sys.modular.datasource.controller;

import com.platform.sys.core.util.GenInitUtil;
import com.platform.sys.modular.datasource.param.DataSourceParam;
import org.springframework.beans.factory.annotation.Value;
import com.platform.core.annotion.BusinessLog;
import com.platform.core.annotion.Permission;
import com.platform.core.enums.LogAnnotionOpTypeEnum;
import com.platform.core.pojo.response.ResponseData;
import com.platform.core.pojo.response.SuccessResponseData;
import com.platform.sys.modular.datasource.service.DataSourceService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


import javax.annotation.Resource;
import java.util.List;

/**
 * 数据源切换控制器
 *
 * @author gyp
 * @date 2022-10-31 14:45:54
 */
@RestController
public class DataSourceController {

    @Resource
    private DataSourceService dataSourceService;

    @Value("${spring.datasource.dynamic.datasource.master.url}")
    private String url;

    @Value("${spring.datasource.dynamic.datasource.master.username}")
    private String username;

    @Value("${spring.datasource.dynamic.datasource.master.password}")
    private String password;

    /**
     * 查询数据源切换
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @GetMapping("/dataSource/page")
    @BusinessLog(title = "数据源切换_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(DataSourceParam dataSourceParam) {
        return new SuccessResponseData(dataSourceService.page(dataSourceParam));
    }

    /**
     * 添加数据源切换
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @PostMapping("/dataSource/add")
    @BusinessLog(title = "数据源切换_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(DataSourceParam.add.class) DataSourceParam dataSourceParam) {
            dataSourceService.add(dataSourceParam);
        return new SuccessResponseData();
    }

    /**
     * 删除数据源切换，可批量删除
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @PostMapping("/dataSource/delete")
    @BusinessLog(title = "数据源切换_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(DataSourceParam.delete.class) List<DataSourceParam> dataSourceParamList) {
            dataSourceService.delete(dataSourceParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑数据源切换
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @PostMapping("/dataSource/edit")
    @BusinessLog(title = "数据源切换_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(DataSourceParam.edit.class) DataSourceParam dataSourceParam) {
            dataSourceService.edit(dataSourceParam);
        return new SuccessResponseData();
    }

    /**
     * 查看数据源切换
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @GetMapping("/dataSource/detail")
    @BusinessLog(title = "数据源切换_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(DataSourceParam.detail.class) DataSourceParam dataSourceParam) {
        return new SuccessResponseData(dataSourceService.detail(dataSourceParam));
    }

    /**
     * 数据源切换列表
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @GetMapping("/dataSource/list")
    @BusinessLog(title = "数据源切换_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(DataSourceParam dataSourceParam) {
        return new SuccessResponseData(dataSourceService.list(dataSourceParam));
    }

    /**
     * 编辑数据源加载
     *
     * @author 郭云鹏
     * @date 2022-07-29 18:30:10
     */
    @PostMapping("/dataSource/enable")
    @BusinessLog(title = "数据源加载_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData enable(@RequestBody @Validated(DataSourceParam.detail.class) DataSourceParam dataSourceParam) {
        dataSourceService.enable(dataSourceParam);
        GenInitUtil.onApplicationEvent(url,username,password);
        return new SuccessResponseData();
    }

    /**
     * 导出系统用户
     *
     * @author gyp
     * @date 2022-10-31 14:45:54
     */
    @Permission
    @GetMapping("/dataSource/export")
    @BusinessLog(title = "数据源切换_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(DataSourceParam dataSourceParam) {
        dataSourceService.export(dataSourceParam);
    }

}
